package com.portal.web.action;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.portal.model.system.User;


/**
 * 用户登陆
 * @author liu.qiao
 *
 */
public class Login extends BaseAction {
	private static final long serialVersionUID = 1L;
	private String name;
	private String password;
	private String errorInfo;

    public String execute() throws Exception {
    	try {
    		User user = new User();
//    		user.setLoginName(this.name);
    		List<User> userlist = this.getServiceContainer().getUserService().getList(user, null);
    		if(userlist.size()<=0){
    			this.setErrorInfo("用户名不存在!");
    			return "login";
    		}else{
    			User dbuser = userlist.get(0);
    			if(!dbuser.getPassword().equals(this.getPassword())){
    				this.setErrorInfo("密码有误!");
        			return "login";
    			}
    			
    			HttpSession session = ServletActionContext.getRequest().getSession();
        	    session.setAttribute("user", dbuser);
    		}
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
        return SUCCESS;
    }

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getErrorInfo() {
		return errorInfo;
	}

	public void setErrorInfo(String errorInfo) {
		this.errorInfo = errorInfo;
	}
}
